package com.example.kaogong.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.kaogong.common.exception.ParamsErrorException;
import com.example.kaogong.common.utils.Assert;
import com.example.kaogong.common.utils.RespBean;
import com.example.kaogong.sys.pojo.dto.QuestionDto;
import com.example.kaogong.sys.pojo.vo.QuestionVo;
import com.example.kaogong.sys.service.IQuestionService;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.Principal;

/**
 * <p>
 * 题目 前端控制器
 * </p>
 *
 * @author    
 * @since 2022-03-26
 */
@RestController
@RequestMapping("/question")
@Api(tags = "试题管理接口")
@Slf4j
public class QuestionController {

    @Autowired
    private IQuestionService questionService;

    @ApiOperation(value = "教师新增一道试题")
    @PostMapping(value = "/manage")
    public RespBean addQuestion(@Valid @RequestBody QuestionDto questionDto, Principal principal) {
        if (principal == null)
            return RespBean.error();
        Assert.notBlank(principal.getName(), new ParamsErrorException("用户不存在"));
        log.info("request:{}", new Gson().toJson(questionDto));
        RespBean respBean = questionService.addQuestion(questionDto, principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "根据试题编号删除试题")
    @DeleteMapping(value = "/manage/{questionId}")
    public RespBean deleteQuestionByQuestionId(@PathVariable Integer questionId) {
        Assert.notNull(questionId, new ParamsErrorException("试题编号不能为空"));
        log.info("questionId:{}", questionId);
        RespBean respBean = questionService.deleteQuestionByQuestionId(questionId);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "分页查询当前教师所出所有试题")
    @GetMapping(value = "/manage/{current}/{size}")
    public RespBean<Page<QuestionVo>> getAllQuestion(@PathVariable Long current, @PathVariable Long size, Principal principal) {
        if (principal == null)
            return RespBean.error();
        Assert.notBlank(principal.getName(), new ParamsErrorException("用户不存在"));
        Assert.notNull(current, new ParamsErrorException("current 不能为空"));
        Assert.notNull(size, new ParamsErrorException("size 不能为空"));
        log.info("current:{}, size:{}", current, size);
        RespBean<Page<QuestionVo>> respBean = questionService.getAllQuestion(current, size, principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "教师修改自己所出试题")
    @PutMapping(value = "/manage")
    public RespBean updateQuestion(@Valid @RequestBody QuestionVo questionVo, Principal principal) {
        if (principal == null)
            return RespBean.error();
        Assert.notBlank(principal.getName(), new ParamsErrorException("用户不存在"));
        log.info("request:{}", new Gson().toJson(questionVo));
        RespBean respBean = questionService.updateQuestion(questionVo, principal.getName());
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }
}
